﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.BusinessRules.ThumbnailHelper;
using System.IO;

public partial class Controls_Club_Add : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitDDLProvince();
            InitDDLCity();
            BindClubType();
        }
    }

    private void BindClubType()
    {
        clubType.DataSource = ClubRules.Instance.GetClubTypes();
        clubType.DataTextField = "texts";
        clubType.DataValueField = "values";
        clubType.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bool addrule;
        bool pri;
        if(addRule.SelectedIndex == 0)
        {
            addrule = true;
        }
        else
        {
            addrule = false;
        }
        if(privacy.SelectedIndex == 0)
        {
            pri = true;
        }
        else
        {
            pri = false;
        }
        string logoFileName = "";
        if (SaveImages(ref logoFileName))
        {
            int clubId = ClubRules.Instance.AddClub(Convert.ToInt32(city.SelectedValue), clubName.Text, clubDes.Text, logoFileName, addrule, pri,Convert.ToInt32(clubType.SelectedValue));
            ClubRules.Instance.AddClubMember(clubId, UserRules.Instance.GetCurrentUserId(), DateTime.Now, ClubRules.m_1LevelMemberTypeId);
            Response.Redirect("/Club/Detail.aspx?clubId="+clubId);
        }
        //up_CreateClubForm.Update();
        
    }

    private bool SaveImages(ref string logo)
    {
        ///'检查文件扩展名字
        errorAlertDiv.Visible = false;
        if (logoUpload.HasFile)
        {
            string fileExt = System.IO.Path.GetExtension(logoUpload.FileName).ToLower();
            string fileName = UserRules.Instance.GetCurrentUserId() + fileExt;

            if (fileExt == ".jpg" || fileExt == ".png" || fileExt == ".gif" || fileExt == ".bmp")
            {
                try
                {
                    logoUpload.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\") +
                        fileName);

                    //ThumbnailGenerater.CreateNewThumbnailImage(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\")
                    //     , fileName
                    //     , System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\S\\")
                    //     , clubName.Text + ".png"
                    //     , 45
                    //     , 45
                    //     , true
                    //     , "");
                    logo = UserRules.Instance.GetCurrentUserId() + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".png";
                    ThumbnailGenerater.CreateNewThumbnailImage(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\")
                         , fileName
                         , System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\")
                         , logo
                         , 105
                         , 135
                         , true
                         , "");
                    File.Delete(System.Web.HttpContext.Current.Request.MapPath("~\\Upload\\Club\\") + fileName);
                    //UserRules.Instance.UpdateCurUserImageSrc();

                    return true;

                }
                catch (Exception ex)
                {
                    errorAlertDiv.Visible = true;
                    lblError.Text = "ERROR: " + ex.Message.ToString();
                    return false;
                }
            }
            else
            {
                errorAlertDiv.Visible = true;
                lblError.Text = "Only .jpg/.png/.gif files allowed!";
                return false;
            }

        }
        else
        {
            logo = "default.png";
            return true;
            //errorAlertDiv.Visible = true;
            //lblError.Text = "You have not specified a file.";
        }
        //return false;
    }

    private void InitDDLProvince()
    {
        province.DataSource = UserRules.Instance.GetProvinces();
        province.DataTextField = "ProvinceName";
        province.DataValueField = "ProvinceId";
        province.DataBind();
    }
    private void InitDDLCity()
    {
        city.DataSource = UserRules.Instance.GetCitiesByProvinceId(Convert.ToInt32(province.SelectedValue.ToString()));
        city.DataTextField = "CityName";
        city.DataValueField = "CityId";
        city.DataBind();
    }
    protected void province_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitDDLCity();
    }
}
